import { cachepromise } from "./cachepromise";
import { validmarkdwoncontenttypes } from "./fetchinbrowser";
export { precheckfetchajaxmarkdown };
const precheckfetchajaxmarkdown = cachepromise(
    async function precheckfetchajaxmarkdown(u: string): Promise<Response> {
        const r = await fetch(u, {
            credentials: "omit",

            method: "HEAD",
            mode: "cors",
        });
        const contenttype = r.headers.get("content-type")?.split(";")[0];
        if (!r.ok) {
            throw new Error("fetch failed:" + "status:" + r.status + "\n" + u);
        } else if (
            !(contenttype && validmarkdwoncontenttypes.includes(contenttype))
        ) {
            throw new Error(
                "fetch failed:" +
                    "content-type:" +
                    r.headers.get("content-type") +
                    "\n" +
                    u
            );
        } else {
            return r;
        }
    }
);
